// TEST1.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "winmain.h"
#include "header.h"
#include <iomanip>

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	Main(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    EOC(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK	 STAT(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);

char objFilePath[255];

// IO
int readline(HWND);

// PDP-8 GUI functions 
void DoFileOpen(HWND);
BOOL LoadTextFileToEdit(HWND, HWND, LPCTSTR);
BOOL LoadObjFile(HWND hwnd, LPCTSTR pszFileName);

int IO( unsigned short inst, HWND hDlg );

// winmain.cpp global variables
bool fileloaded = FALSE;	//indicate if an object file is loaded
bool continuous = FALSE;	//indictate if continuous run mode is up
int step = 0;
bool eoc = FALSE;			//indicate the end of code
bool opennew = FALSE;
int selbase = 0;			//starting address for list box for lighting

unsigned short inst = 0;	//store current instruction


BOOL LoadTextFileToEdit(HWND hEdit, HWND hwnd, LPCTSTR pszFileName)
{
	HANDLE hFile;			      // J:handle for the file opened
	BOOL bSuccess = FALSE; 
	char inbuf;
	char *inlinebuf = new char[80];
	int z,x;
	int fileindex = 0;
	int lineindex = 0;

	ifstream fin;

	//fin.open("C:\\Users\\Eugene\\Documents\\My Classes\\ECE486\\ece486projectsvn\\codes\\GUITest\\add01.obj");

	//int b;

	//fin.setf(ios_base::oct, ios_base::basefield);

	//fin >> b;

	//fin.close();

	//fin.open(pszFileName);

	//fin.close();

	//do
	//{
	//	do
	//	{
	//		fin.get(inbuf);
	//		fin.get();
	//		inlinebuf[lineindex++] = inbuf;
	//	} while (pszFileText[fileindex] && pszFileText[fileindex] != '\n');
	//	
	//} while (!fin.eof());

	


	hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
		OPEN_EXISTING, 0, NULL);  // J:open the file

	if(hFile != INVALID_HANDLE_VALUE)
	{
		DWORD dwFileSize;                     // J:dwFileSize stores the
		dwFileSize = GetFileSize(hFile, NULL);// J:size of the file opened
		if(dwFileSize != 0xFFFFFFFF)
		{
			//LPSTR pszFileText;
			//pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);

			char *pszFileText = new char[dwFileSize + 1]; //input buffer

			if(pszFileText != NULL)
			{
				DWORD dwRead;

				if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
				{
					pszFileText[dwFileSize] = 0; // Add null terminator

					char *codeline = new char[100]; //store a line of as code
					//int fileindex = 0;
					//int lineindex = 0;
												  //read from input buffer
					while(pszFileText[fileindex]) //and send to listbox
					{
						do
						{
							codeline[lineindex++] = pszFileText[fileindex++];
						} while (pszFileText[fileindex] && pszFileText[fileindex] != '\n');

						fileindex++;
						codeline[lineindex] = 0;

						if((codeline[0] != '/') && (codeline[0] != '\n)' && (codeline[0] != '*')))
							SendDlgItemMessage(hwnd, IDC_LISTSOURCE, LB_ADDSTRING, 0, (LPARAM)codeline);

						lineindex = 0;
					}

					/*****************************************************************
					//Generate the full path of pal.exe, execute and create .obj file
					*****************************************************************/

					char *cwd = new char[300];  //store current working directory
					_getcwd(cwd, 299);			//read current working directory
					int lencwd = strlen(cwd);      //length of cwd
					
					char *palexe = new char[10]; //store string pal.exe
					strcpy_s(palexe,9,"\\pal.exe");

					//char *pwd = new char[200];  //store the full path of pal.exe
					char *pwd = new char[lencwd+10];

					strcpy_s(pwd, lencwd+10, cwd);

					strcat_s(pwd, lencwd+10, palexe);

					//Generate two arguments for pal.exe
					//e.g. pal.exe -o add01.obj
					
					char arg1[] = " -o";		//pal.exe -o option for octal
			
					int pszFileNameSize = strlen(pszFileName);
					
					char *arg2 = new char[pszFileNameSize+3]; //.as file path

					strcpy_s(arg2,pszFileNameSize+3, "\"");
					strcat_s(arg2,pszFileNameSize+3, pszFileName);
					strcat_s(arg2,pszFileNameSize+3, "\"");   //append double quotes
					
					_spawnl(_P_WAIT, pwd, arg1, arg2, NULL);  //execute pal.exe with args
					

					/*****************************************************************
					//Read data from .obj file created
					****************************************************************/
					
					//create .obj filename
					char *objFileName = new char[strlen(pszFileName)+2];
					strcpy_s(objFileName,strlen(pszFileName)+2, pszFileName);

					objFileName[strlen(pszFileName)-2] = 'o'; //substitute .as 
					objFileName[strlen(pszFileName)-1] = 'b'; //with .obj
					objFileName[strlen(pszFileName)] = 'j';
					objFileName[strlen(pszFileName)+1] = 0;

					LoadObjFile(hwnd,objFileName);

					////read data from .obj file

					//FILE *pFile;
					//FILE **ppFile = &pFile;

					//fopen_s(ppFile, objFileName, "r");

					//unsigned long tmplong;
					//unsigned long tmplongmsk = 077;
					//unsigned short ocode;
					//unsigned short ocodetmp;
					//unsigned short memindex;			//store starting memory address

					////read the first line = starting memory address

					////read the lower 6 bits

					//char *pfread = new char[10];		//input buffer

					//fgets (pfread, 9, pFile);			//read first line from file

					//tmplong = strtoul(pfread, NULL, 8); //convert to long int
					//tmplong = tmplong & tmplongmsk;		//mask with 077o

					//ocodetmp = (unsigned short)tmplong; //cast to unsigned short
					//ocode = ocodetmp << 6;				//shift 6 bits to the left 

					//fgets (pfread, 10, pFile);

					////read the upper 6 bits

					//tmplong = strtoul(pfread, NULL, 8);
					//tmplong = tmplong & tmplongmsk;

					//ocodetmp = (unsigned short)tmplong;

					////combine lower and upper 6 bits

					//ocode = ocode | ocodetmp;			//bitwise and operation

					//memindex = ocode;

					//sprintf(pfread,"%04o",memindex);	//covert index to string

					//SetWindowText(hEdit, pfread);

					//SendDlgItemMessage(hwnd, IDC_LISTMACHINE, LB_ADDSTRING, 0, (LPARAM)pfread);

					////read all lines and store to memory[4096] array

					//while(!feof(pFile))
					//{
					//	fgets (pfread, 10, pFile);

					//	tmplong = strtoul(pfread, NULL, 8);
					//	tmplong = tmplong & tmplongmsk;

					//	ocodetmp = (unsigned short)tmplong;
					//	ocode = ocodetmp << 6;

					//	fgets (pfread, 10, pFile);

					//	tmplong = strtoul(pfread, NULL, 8);
					//	tmplong = tmplong & tmplongmsk;

					//	ocodetmp = (unsigned short)tmplong;

					//	ocode = ocode | ocodetmp;

					//	memory[memindex++] = ocode;

					//	sprintf(pfread,"%04o",ocode);

					//	SendDlgItemMessage(hwnd, IDC_LISTMACHINE, LB_ADDSTRING, 0, (LPARAM)pfread);
					//}

					bSuccess = TRUE; // It worked!

				}
				GlobalFree(pszFileText);
			}
		}
		CloseHandle(hFile);
	}
	return bSuccess;
}



// Load PDP-8 Obj Code and Initialize registers

BOOL LoadObjFile(HWND hwnd, LPCTSTR pszFileName)
{
	BOOL loadResult = FALSE;  

	unsigned int upper, lower;
	unsigned short word, memindex;

	strcpy_s(objFilePath,254,pszFileName);

	memindex = 0;
	upper = 0;
	lower = 0;
	word  = 0;
	int result = 0;
	int i = 0;

	ifstream fin;

	char *outbuf = new char[100];
	string tmpstring;

	//int total = SendDlgItemMessage(hwnd, IDC_LISTMACHINE2, LB_GETCOUNT, 0, 0);

	// Clear all listboxes before loading object file
	SendDlgItemMessage(hwnd, IDC_MEM, LB_RESETCONTENT, (WPARAM)0, (LPARAM)0);
	SendDlgItemMessage(hwnd, IDC_LISTMACHINE, LB_RESETCONTENT, (WPARAM)0, (LPARAM)0);
	
	fin.open(pszFileName);

	fin.setf( ios_base::oct, ios_base::basefield );

	// Read in the first line of the object file
	fin >> upper;
	fin.get();
	fin >> lower;
	fin.get();

	if(!fin.eof())
	{
		initialize();		//initialize all variables
		fileloaded = TRUE;
		eoc = FALSE;

		// Load PC with the first instruction
		// assuming that the fist line is starting address
		PC = (upper << 6 | lower) & 0007777;
		selbase = PC;
		
		do
		{
			word = upper << 6 | lower;
			word &= 0007777;

			if(!(upper & 000000100))
			{
				memory[memindex++] = word;

				sprintf_s(outbuf,20,"%04o  %04o",memindex-1, word);
			
				SendDlgItemMessage(hwnd, IDC_LISTMACHINE, LB_ADDSTRING, 0, (LPARAM)outbuf);
			}
			else
				memindex = word;

			fin >> upper;
			fin.get();
			fin >> lower;
			fin.get();

		} while(!fin.eof());

		updatedisplay(hwnd);

		SendDlgItemMessage(hwnd, IDC_LISTMACHINE, LB_SETCURSEL, PC-selbase, 0);
	}
	else
	{
		//cout << "\n*Object File Empty*\n\n";
		result = 1;
	}

	fin.close();

	/*if(debug)
		displaymemory(1);*/

	for (i=0; i<4096; ++i)
	{
		sendtoIDC_MEM(hwnd, memory[i], i);
	}
		SendDlgItemMessage(hwnd, IDC_MEM, LB_SETCARETINDEX, PC+16, (LPARAM)0);

	return loadResult;
}


void DoFileOpen(HWND hwnd)
{
	OPENFILENAME ofn;
	char szFileName[MAX_PATH] = "";

	ZeroMemory(&ofn, sizeof(ofn));

	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFilter = "Assembly Files (*.as)\0*.as\0All Files (*.*)\0*.*\0";
	ofn.lpstrFile = szFileName;
	ofn.nMaxFile = MAX_PATH;
	ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
	ofn.lpstrDefExt = "as";

	if(GetOpenFileName(&ofn))
	{
		HWND hEdit = GetDlgItem(hwnd, IDC_EDIT1);
		LoadTextFileToEdit(hEdit, hwnd, szFileName);
	}
}



// Open PDP-8 .obj file in the file system
void DoFileOpen2(HWND hwnd)
{
	OPENFILENAME ofn;
	char szFileName[MAX_PATH] = "";

	ZeroMemory(&ofn, sizeof(ofn));

	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFilter = "Obj Files (*.obj)\0*.obj\0All Files (*.*)\0*.*\0";
	ofn.lpstrFile = szFileName;
	ofn.nMaxFile = MAX_PATH;
	ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
	ofn.lpstrDefExt = "as";

	if(GetOpenFileName(&ofn))
		LoadObjFile(hwnd, szFileName);
}




// WinMain - Call IDD_MAIN
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	return (int) DialogBox(hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, Main);
}




//
//  FUNCTION: (HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main dialog box.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//

// Message handler for about box.



// Main loop for the PDP-8 datapath
int mainloop(HWND hwnd)
{
	int ishalt = 0;

	debug = DEBUG;
	debugparam = DEBUGPARAM;
	
	if(fileloaded)
	{

		do
		{
			inst = fetch();

			ishalt = decode(inst,hwnd);

			if(IR <= 6)
			{
				unsigned short EAddr = calEAddrWinmain(inst);
				updateIDC_MEM(hwnd, EAddr);
			}
			else;
			
			updatedisplay(hwnd);
			SendDlgItemMessage(hwnd, IDC_MEM, LB_SETCARETINDEX, PC+16, (LPARAM)0);

			if(step)
				--step;

		} while(!ishalt && (continuous || step));

		if ( ishalt)
		{
			continuous = FALSE;
			fileloaded = FALSE;
			step = 0;
			eoc = TRUE;
			DialogBox(hInst, MAKEINTRESOURCE(IDD_STAT), NULL, STAT);
		}

		//if(debug)
		//	displaymemory(2);

		//displaystatistics();
	}
	else;
	
	return 0;
}


// Main - process messages from user 
INT_PTR CALLBACK Main(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);

	//int index = SendDlgItemMessage(hDlg, IDC_LISTSOURCE, LB_ADDSTRING, 0, (LPARAM)defaultSource);


	//SetDlgItemText(hDlg, IDC_INT, defaultMachine);

	switch (message)
	{
	case WM_INITDIALOG:

		return (INT_PTR)TRUE;


	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		else if (LOWORD(wParam) == IDC_STBTN)
		{
			int len = GetWindowTextLength(GetDlgItem(hDlg, IDC_STRING));
			if(len > 0)
			{
				char* buf;

				buf = new char[len];
				GetDlgItemText(hDlg, IDC_STRING, buf, len + 1);

				int index = SendDlgItemMessage(hDlg, IDC_LISTSOURCE, LB_ADDSTRING, 0, (LPARAM)buf);

				//buf = (char*)GlobalAlloc(GPTR, len + 1);
				//GetDlgItemText(hwnd, IDC_TEXT, buf, len + 1);
			}
			else;
		}
		else if (LOWORD(wParam) == IDC_LDBTN)
			DoFileOpen(hDlg);
		else if (LOWORD(wParam) == IDC_LDBTN2)
			DoFileOpen2(hDlg);
		else if (LOWORD(wParam) == IDC_STEPBTN)
		{
			if(!eoc && fileloaded)
			{
				//int total = SendDlgItemMessage(hDlg, IDC_LISTMACHINE, LB_GETCOUNT, 0, 0);

				//SendDlgItemMessage(hDlg, IDC_LISTMACHINE, LB_GETCOUNT, 0, 0);

				/*LPARAM position;
				position = listboxindex++;*/

				step = 1;
				
				mainloop(hDlg);
			}
			else if (eoc)
			{
				eoc = FALSE;

				DialogBox(hInst, MAKEINTRESOURCE(IDD_EOC), NULL, EOC);

				if(opennew)
					DoFileOpen2(hDlg);

				opennew = FALSE;
			}
		}
		else if (LOWORD(wParam) == IDC_RUN)
		{
			if(!eoc && fileloaded)
			{
				continuous = TRUE;

				mainloop(hDlg);

			}
			else if (eoc)
			{
				eoc = FALSE;

				DialogBox(hInst, MAKEINTRESOURCE(IDD_EOC), NULL, EOC);

				if(opennew)
					DoFileOpen2(hDlg);

				opennew = FALSE;
			}
		}
		else if (LOWORD(wParam) == ID_SND)
		{
			readline(hDlg);
		}
		else if (LOWORD(wParam) ==  IDC_STP5)
		{
			if(!eoc && fileloaded)
			{
				step = 5;

				mainloop(hDlg);

			}
			else if (eoc)
			{
				eoc = FALSE;

				DialogBox(hInst, MAKEINTRESOURCE(IDD_EOC), NULL, EOC);

				if(opennew)
					DoFileOpen2(hDlg);

				opennew = FALSE;
			}
		}

		break;
	}
	return (INT_PTR)FALSE;
}

INT_PTR CALLBACK EOC(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:

		return (INT_PTR)TRUE;


	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK)
		{
			EndDialog(hDlg, LOWORD(wParam));
			opennew = TRUE;
		}
		else if (LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			opennew = FALSE;
		}

	}
	return (INT_PTR)FALSE;
}


// Calculate effective address for GUI use
unsigned short calEAddrWinmain(unsigned short inst)
{
	unsigned short EAddr;
	unsigned short offset;
	unsigned short CPC = PC - 1;


	EAddr = 0;
	offset = inst & 00177;
	if(IR <=5)
	{
	switch (inst & 00600)
	{
		case 00000:	// Zero Page Addressing OR Autoindexing
					EAddr |= offset; // Zero Page Addressing
					break;

		case 00200: // Current Page Addressing
					EAddr = (CPC & 07600) | offset;	// Mask low order 7 bits of PC
													// then concatenate with offset
					break;

		case 00400: // Indirect Addressing with Zero page
					if (offset >= 00010 && offset <= 00017) // Autoiindexing
						EAddr =memory[offset] + 1 - 1;
					else
						EAddr = memory[offset];

					break;

		case 00600: // Indirect Addressing with Current page
					EAddr = memory[(CPC & 07600) | offset];
					break;
					
		default:;
	}
	}

	return EAddr;
}

// Send the data to the list box control for the memory
void sendtoIDC_MEM(HWND hDlg, unsigned short data, int i)
{
	char *outbuf = new char[30];

	sprintf_s(outbuf,20,"[%04o] %04o", i, data);

	SendDlgItemMessage(hDlg, IDC_MEM, LB_ADDSTRING, 0, (LPARAM)outbuf);

	return;
}

// Update content of the memory at EAddr
void updateIDC_MEM(HWND hDlg, int EAddr)
{
	char *outbuf = new char[30];
	unsigned int offset = inst & 00177;

	sprintf_s(outbuf,20,"[%04o] %04o",offset, memory[offset]);

	SendDlgItemMessage(hDlg, IDC_MEM, LB_DELETESTRING, offset, (LPARAM)0);
	SendDlgItemMessage(hDlg, IDC_MEM, LB_INSERTSTRING, offset, (LPARAM)outbuf);

	sprintf_s(outbuf,20,"[%04o] %04o",EAddr, memory[EAddr]);

	SendDlgItemMessage(hDlg, IDC_MEM, LB_DELETESTRING, EAddr, (LPARAM)0);
	SendDlgItemMessage(hDlg, IDC_MEM, LB_INSERTSTRING, EAddr, (LPARAM)outbuf);

	return;
}


// Update text box control box for PC reg
void updateIDC_PC(HWND hDlg)
{
	char *outbuf = new char[10];

	sprintf_s(outbuf,9,"%04o", PC);

	SetDlgItemText(hDlg, IDC_PC, outbuf);

	return;
}

// Update text box control for Instruction
void updateINST(HWND hDlg)
{
	char *outbuf = new char[10];

	sprintf_s(outbuf,9,"%04o", inst);

	SetDlgItemText(hDlg, IDC_INST, outbuf);

	return;
}

// Update GUI
void updatedisplay(HWND hDlg)
{
	unsigned short EAddr = calEAddrWinmain(inst);
	char *selPC = new char[10];

	updateIDC_PC(hDlg);
	updateIDC_AC(hDlg);
	updateIDC_IR(hDlg);

	updateINST(hDlg);
	
	updateEAD(hDlg, EAddr);

	//updateIDC_MEM(hDlg, EAddr);

	sprintf_s(selPC,9,"%04o",PC);

	//SendDlgItemMessage(hDlg, IDC_LISTMACHINE, LB_SETCURSEL, PC-selbase, 0);
	SendDlgItemMessage(hDlg, IDC_LISTMACHINE, LB_SELECTSTRING, (WPARAM)-1, (LPARAM)selPC);
	SendDlgItemMessage(hDlg, IDC_LISTSOURCE, LB_SELECTSTRING, (WPARAM)-1, (LPARAM)selPC);

	if(IR <=6 )
		SendDlgItemMessage(hDlg, IDC_MEM, LB_SETCURSEL, EAddr, 0);

	return;
}

// Update text box controlfor EAddr
void updateEAD(HWND hDlg, unsigned short EAddr)
{
	char *outbuf = new char[10];

	sprintf_s(outbuf,9,"%04o", EAddr);

	SetDlgItemText(hDlg, IDC_EAD, outbuf);

	return;
}

// Update text box controlfor Acculmulator reg
void updateIDC_AC(HWND hDlg)
{
	char *outbuf = new char[10];

	sprintf_s(outbuf,9,"%05o", LB_AC);

	SetDlgItemText(hDlg, IDC_AC, outbuf);
	
	return;
}


// Update text box controlfor IR reg
void updateIDC_IR(HWND hDlg)
{
	char *outbuf = new char[10];

	sprintf_s(outbuf,9,"%01o", IR);

	SetDlgItemText(hDlg, IDC_IR, outbuf);
	return;
}


// Display simulation statistics
int displaystatistics(HWND hDlg)
{
	//The total number of instructions executed
    //The total number of clock cycles consumed
    //The number of times each instruction type was executed

	char *outbuf = new char[100];

	//Calculate total Cycle Count
	totalCycCount = (ANDcount + TADcount + ISZcount + DCAcount + JMScount) * 2 + JMPcount + microcount + extraMemCycCount;

	strcpy_s(outbuf,50,"--------------STATISTICS--------------\n");
	appendresult(hDlg, outbuf);

	sprintf_s(outbuf,50," Total Instructions: %d \n", totalInst);
	appendresult(hDlg, outbuf);

	sprintf_s(outbuf,50,"  AND  : %4d\n",  ANDcount );
	appendresult(hDlg, outbuf);

	sprintf_s(outbuf,50,"  TAD  : %4d\n",  TADcount );
	appendresult(hDlg, outbuf);

	sprintf_s(outbuf,50,"  ISZ  : %4d\n",  ISZcount );
	appendresult(hDlg, outbuf);

	sprintf_s(outbuf,50,"  DCA  : %4d\n",  DCAcount );
	appendresult(hDlg, outbuf);

	sprintf_s(outbuf,50,"  JMS  : %4d\n",  JMScount );
	appendresult(hDlg, outbuf);

	sprintf_s(outbuf,50,"  JMP  : %4d\n",  JMPcount );
	appendresult(hDlg, outbuf);

	sprintf_s(outbuf,50,"  IO   : %4d\n",  IOcount );
	appendresult(hDlg, outbuf);

	sprintf_s(outbuf,50,"  Micro: %4d\n",  microcount );
	appendresult(hDlg, outbuf);

	sprintf_s(outbuf,50,"Total Cycles :%4d\n",  totalCycCount );
	appendresult(hDlg, outbuf);

	//strcpy_s(outbuf,50,"---------------------------------------\n");
	//appendresult(hDlg, outbuf);

	//strcpy_s(outbuf,50,"**************STATISTICS**************");
	//SendDlgItemMessage(hDlg, IDC_RST, LB_ADDSTRING, 0, (LPARAM)outbuf);

	//sprintf_s(outbuf,50," Total Instructions: %d", totalInst);
	//SendDlgItemMessage(hDlg, IDC_RST, LB_ADDSTRING, 0, (LPARAM)outbuf);

	//sprintf_s(outbuf,50,"  AND  : %d",  ANDcount );
	//SendDlgItemMessage(hDlg, IDC_RST, LB_ADDSTRING, 0, (LPARAM)outbuf);

	//sprintf_s(outbuf,50,"  TAD  : %d",  TADcount );
	//SendDlgItemMessage(hDlg, IDC_RST, LB_ADDSTRING, 0, (LPARAM)outbuf);

	//sprintf_s(outbuf,50,"  ISZ  : %d",  ISZcount );
	//SendDlgItemMessage(hDlg, IDC_RST, LB_ADDSTRING, 0, (LPARAM)outbuf);

	//sprintf_s(outbuf,50,"  DCA  : %d",  DCAcount );
	//SendDlgItemMessage(hDlg, IDC_RST, LB_ADDSTRING, 0, (LPARAM)outbuf);

	//sprintf_s(outbuf,50,"  JMS  : %d",  JMScount );
	//SendDlgItemMessage(hDlg, IDC_RST, LB_ADDSTRING, 0, (LPARAM)outbuf);

	//sprintf_s(outbuf,50,"  JMP  : %d",  JMPcount );
	//SendDlgItemMessage(hDlg, IDC_RST, LB_ADDSTRING, 0, (LPARAM)outbuf);

	//sprintf_s(outbuf,50,"  IO   : %d",  IOcount );
	//SendDlgItemMessage(hDlg, IDC_RST, LB_ADDSTRING, 0, (LPARAM)outbuf);

	//sprintf_s(outbuf,50,"  Micro: %d",  microcount );
	//SendDlgItemMessage(hDlg, IDC_RST, LB_ADDSTRING, 0, (LPARAM)outbuf);

	//sprintf_s(outbuf,50,"Total Cycles :%d",  totalCycCount );
	//SendDlgItemMessage(hDlg, IDC_RST, LB_ADDSTRING, 0, (LPARAM)outbuf);

	//strcpy_s(outbuf,50,"***************************************");
	//SendDlgItemMessage(hDlg, IDC_RST, LB_ADDSTRING, 0, (LPARAM)outbuf);

	return 0;
}

INT_PTR CALLBACK STAT(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:

		displaystatistics(hDlg);

		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK)
		{
			EndDialog(hDlg, LOWORD(wParam));

		}
	}
	return (INT_PTR)FALSE;
}

void appendresult(HWND hDlg, char*outbuf)
{
	HWND hEdit = GetDlgItem (hDlg, IDC_RST);
	int ndx = GetWindowTextLength (hEdit);

	SendMessage (hEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx);
	SendMessage (hEdit, EM_REPLACESEL, 0, (LPARAM) outbuf);

	return;
}